package com.demo.controller;


import com.alibaba.fastjson.JSON;
import com.demo.po.ResultDto;
import com.demo.service.VideoParseUrlService;
import com.demo.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;



@Slf4j
@Controller
public class ParserController {

    @Autowired
    private VideoParseUrlService videoParseUrlService;

    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    @RequestMapping("/parseVideoUrl")
    @ResponseBody
    public String parseVideoUrl(@RequestBody String url) throws Exception {

        log.info("待解析URL :{}", url);

        URLDecoder.decode(url, "utf-8");

        ResultDto resultDto = new ResultDto();
        try {
            url = URLDecoder.decode(url).replace("url=", "");

            if (url.contains(CommonUtils.HUO_SHAN_DOMAIN)) {

                resultDto = videoParseUrlService.hsParseUrl(url);

            } else if (url.contains(CommonUtils.DOU_YIN_DOMAIN)) {

                resultDto = videoParseUrlService.dyParseUrl(url);
            }
        } catch (Exception e) {

            log.error("去水印异常 {}", e);
        }
        return JSON.toJSONString(resultDto);
    }


    @PostMapping("/async_data")
    @ResponseBody
    public Object async_data(HttpServletRequest request, String url, String desc, HttpServletResponse response) {
        try {
            URLDecoder.decode(url, "UTF-8");
            CommonUtils.doGet(response, url, desc);
        } catch (Exception e) {
            log.error("代理资源地址 失败 {}", e.getMessage());
        }
        return null;
    }
}